<template>
	<view class="page">
		<view class="user">
			
			<view class="" style="display: flex;justify-content: space-between;align-items: center;width: 100%;">
				<view class="" style="display: flex;">
					<image :src="info.app.cdnurl+userInfo.avatar" mode="widthFix" />
					<view class="user-info">
						<text>{{userInfo.nickname}}</text>
						<text v-if="options && options.vipInfo && options.vipInfo.expiredate">{{options.vipInfo.expiredate}}</text>
					</view>
				</view>
				<view class="" style="background: #F9D7B2;padding:15rpx 30rpx;border-radius: 30rpx;color: #333;font-weight: bold;font-size: 28rpx;" @click="vipLog">vip记录</view>
			</view>
		</view>

		<view class="content">
			<view class="title">会员开通方案</view>
			<view class="option">
				<view v-for="(v, k) in orderList" :class="'option-item ' + (k === current ? 'option-selected' : '')"
					:key="k" @click="change(k)">
					<text class="option-item-time">{{v.subtext}}</text>
					<view class="option-item-price"><text>￥</text><text>{{v.price}}</text></view>
					<view class="option-item-tip">{{ v.title }}</view>
				</view>

				<view style="width: 225rpx;"></view>
				<view style="width: 225rpx;"></view>
			</view>
			<view class="button" @click="payBtn()">
				<text v-if="orderList && orderList[current]">立即以{{orderList[current].price || 0}}元续费</text>
			</view>
			<view class="title" style="color: #777; font-size: 14px;">注意事项</view>
			<view class="notice">
				<text>1. 会员权益与手机号相对应,无共享账号</text>
				<text>2. 开通会员前请阅读《用户协议》,会员服务一经开通后不可退款</text>
				<text>3. 严禁使用任何手段爬取本产品数据,一经发现可能面临账号禁用风险</text>
			</view>
		</view>
		<t-pay-way :show="show" :payMoney="detail.price" v-if="detail && detail.price" @pay="pay"
			@close="popupClose"></t-pay-way>
	</view>
</template>

<script>
	import {
		getUserProfile,
		getVipIndex,
		getTradeOrderVip,
		getTradeOrderDetail,
		getShopPayPrepay,
		postWechatBind
	} from "@/apis/index"
	import tPayWay from "@/components/t-pay-way/t-pay-way"
	export default {
		components: {
			tPayWay
		},
		data() {
			return {
				show: false,
				options: [],
				advantages: [],
				current: 0, // 当前选中套餐下标
				userInfo: {},
				detail: {},
				orderList: {},
				orderDetail:{}
			}
		},
		onLoad() {
			this.getUserInfo()
			this.getDataList()
		},
		onshow() {},
		methods: {
			vipLog(){
				uni.navigateTo({
						url:"/templatePage/vipLog/vipLog"
					})
			},
			getUserInfo() {
				getUserProfile().then(res => {
					this.userInfo = res.data
					console.log(this.userInfo, 'this.userInfo')
				})
			},
			getDataList() {
				getVipIndex().then(res => {
					this.options = res.data

					this.orderList = res.data.vipList[0].pricedata
					console.log(this.orderList[0].price, 'this.orderListthis.orderListthis.orderList')
				})
			},
			change(k) {
				this.current = k;
			},
			async payBtn() {
				this.detail = this.options.vipList[0].pricedata[this.current]
				this.detail.level = this.options.vipList[0].level
				
				// if (this.options.vipInfo.id) {
				// 	this.detail.vip_id= this.options.vipInfo.id
				// }
				
				
				console.log(`点击支付`, this.options.vipList[0].pricedata[this.current])
				let order = await getTradeOrderVip(this.detail)
				if (order.code == 1) {
					let res = await getTradeOrderDetail(order.data.order_sn)
					this.orderDetail = res.data
				}
				this.show = true
			},
			popupClose() {
				this.show = false
			},
			async pay(e) {

				let orderType
				if (e == 1) {
					orderType = 'money'
				}
				if (e == 2) {
					orderType = 'wechat'
				}
				if (e == 3) {
					orderType = 'wechat'
				}
				getShopPayPrepay({
					payment: orderType,
					order_sn: this.orderDetail.order_sn
				}).then(res => {
					uni.showLoading({
						title: '支付中'
					})
					if (res.data == -1 && res.msg == 'miss_openid') {
						uni.showModal({
							title: '微信支付',
							content: '请先绑定微信再使用微信支付',
							success: function (res) {
								if (res.confirm) {
									postWechatBind().then(res=>{
										uni.showToast({
											title: res.msg,
											icon: 'none'
										})
									})
								}
							},
						});
					}
					
					
					if (res.code == 1) {
						// #ifdef H5
						uni.navigateTo({
							url: "/discountPage/success/success"
						})
						// #endif
						// #ifdef MP-WEIXIN
						uni.requestPayment({
							provider: 'wxpay',
							...res.data.pay_data,
							success: (res) => {
								// that.payResult('success');
								// uni.navigateTo({
								// 	url: "/discountPage/success/success"
								// })
								console.log(res,'---------------')
								uni.hideLoading()
								uni.showToast({
										title: '支付成功',
										icon: 'none'
									})
								this.show = false
							},
							fail: (err) => {
								if (err.errMsg === 'requestPayment:fail cancel') {
									uni.showToast({
										title: '取消支付',
										icon: 'none'
									})
									this.show = false
								} else {
									// that.payResult('fail');
								}
							},
						});
						// #endif
					}
					this.show = false


				})

			}
		},


	}
</script>

<style lang="scss">
	page {
		background: #53536B;
	}

	.page {
		width: 100vw;
		padding-top: 50rpx;

		.user {
			display: flex;
			align-items: center;
			background: #DCDDE0;
			width: 700rpx;
			margin: auto;
			box-sizing: border-box;
			padding: 0 30rpx;
			border-radius: 20rpx 20rpx 0 0;
			height: 160rpx;

			image {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
			}

			.user-info {
				height: 90rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				padding-left: 15rpx;

				text {
					&:nth-child(1) {
						font-weight: bold;
					}

					&:nth-child(2) {
						font-size: 14px;
						color: #777;
					}
				}
			}
		}

		.content {
			background: #fff;
			border-radius: 20rpx 20rpx 0 0;
			padding: 20rpx;
			box-sizing: border-box;
			width: 100vw;

			.title {
				font-weight: bold;
				height: 80rpx;
				line-height: 80rpx;
			}

			.option {
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;

				.option-item {
					width: 225rpx;
					height: 280rpx;
					box-sizing: border-box;
					border: 1px solid #999;
					border-radius: 15rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					position: relative;
					margin-top: 10rpx;

					&:active {
						background: #FFF6ED;
						border-color: #F9D7B2;
					}

					.option-item-time {
						font-weight: bold;
						font-size: 14px;
						color: #555;
					}

					.option-item-price {
						font-weight: bold;
						margin: 20rpx 0;

						text {
							&:nth-child(2) {
								font-size: 26px;
							}
						}
					}

					.option-item-tip {
						font-size: 12px;
						color: #999;
					}

					.option-item-tag {
						position: absolute;
						left: -5rpx;
						top: -10rpx;
						font-weight: bold;
						font-size: 14px;
						padding: 5rpx 10rpx;
						border-radius: 12rpx;
						background: red;
						color: #fff;
					}
				}

				.option-selected {
					background: #FFF6ED;
					border-color: #F9D7B2;

					.option-item-price {
						color: #333;
					}
				}
			}

			.button {
				background: #F9D7B2;
				height: 100rpx;
				border-radius: 50rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				margin-top: 30rpx;

				&:active {
					background: #ceb295;
				}

				text {
					&:nth-child(1) {
						font-weight: bold;
						color: #333;
						line-height: 1.5;
					}

					&:nth-child(2) {
						color: #555;
						font-size: 14px;
					}
				}
			}

			.agreement {
				font-size: 14px;
				text-align: center;
				margin-top: 20rpx;
				margin-bottom: 60rpx;
				color: #f5984a;

				&:active {
					color: #9c602f;
				}
			}

			.advantage {
				display: flex;
				flex-wrap: wrap;

				.advantage-item {
					display: flex;
					width: 350rpx;
					margin-bottom: 20rpx;

					.advantage-item-icon {
						border: 1px solid #F9D7B2;
						border-radius: 50%;
						width: 70rpx;
						height: 70rpx;

						image {
							margin: 10rpx;
							width: 50rpx;
							height: 50rpx;
						}
					}

					.advantage-item-text {
						display: flex;
						flex-direction: column;
						justify-content: center;
						margin-left: 15rpx;

						text {
							&:nth-child(1) {
								font-size: 14px;
								color: #f5984a;
							}

							&:nth-child(2) {
								font-size: 12px;
								color: #888;
							}
						}
					}
				}
			}

			.notice {
				display: flex;
				flex-direction: column;

				text {
					font-size: 12px;
					color: #888;
					margin-bottom: 10rpx;
				}
			}
		}
	}
</style>